package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.ReplyDAO;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.service.ReplyService;
import com.mysteel.page.Page;
@Service("replyService")
public class ReplyServiceImpl implements ReplyService
{
	@Autowired
	private ReplyDAO replyDao;
	
	@Override
	public void reply(Reply reply)
	{
		Assert.notNull (reply);
		
		reply.setReplyTime (System.currentTimeMillis ());
		
		replyDao.save (reply);
	}
	
	@Override
	public void deleteReplyById(long id)
	{
		Reply reply = replyDao.get (id);
		
		if(ObjectUtils.notNull (reply))
		{
			replyDao.delete (id);
		}
	}
	
	@Override
	public Reply getReplyById(long id)
	{
		
		return replyDao.get (id);
	}
	
	@Override
	public Page<Reply> queryReply(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<Reply> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = replyDao.count(params);

		if (count > 0)
		{
			page = new Page<Reply>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> ReplyIds = replyDao.query(params);

			if (ObjectUtils.notEmpty(ReplyIds))
			{
				List<Reply> Replys = new ArrayList<Reply>(ReplyIds.size());

				for (long ReplyId : ReplyIds)
				{
					Replys.add(getReplyById(ReplyId));
				}

				page.setDatas(Replys);
			}
		}

		return page;
	}
	
}
